package com.nettest.common;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

import com.nettest.entity.system.Dictionary;
import com.nettest.entity.system.Staff;
import com.nettest.service.event.DictionaryUpdatedEvent;
import com.nettest.service.event.LoginUserCreatedEvent;
import com.nettest.service.system.DictionaryService;
import com.nettest.service.system.StaffService;

/**
 * @author Nick Mao
 * @date 2013-03-27
 * 
 */
@Service
public class DictionaryData implements ApplicationListener<ApplicationEvent> {
	
	public final static String STAFF_CATEGORY = "STAFF";
	public final static String STORE_CATEGORY = "STORE";
	public final static String DICTIONARY_CATEGORY = "DICTIONARY_CATEGORY";
	public final static String ENTRY_STOCK_FLAG_CATEGORY = "ENTRY_STOCK_FLAG";
	
	private static boolean needRefresh = true;

	private Map<String,List<Dictionary>> dictionaryMap = new HashMap<String, List<Dictionary>>();
    
    private List<Dictionary> dictionaries;
    
    @Autowired
    private DictionaryService dictionaryService;
    
    @Autowired
    private StaffService staffService;
	@PostConstruct
	private synchronized void init() {
		if (needRefresh) {
			Map<String, List<Dictionary>> newDictionaryMap = new HashMap<String, List<Dictionary>>();
			try {
				List<Dictionary> dictionary = getDictionaries();
				for (Dictionary dict : dictionary) {
					if (dict.getCategory() == null) {
						continue;
					}
	
					List<Dictionary> list = newDictionaryMap.get(dict.getCategory());
					if (list == null) {
						list = new ArrayList<Dictionary>();
					}
					list.add(dict);
					newDictionaryMap.put(dict.getCategory(), list);
				}
				
				Dictionary d;
				List<Dictionary> dictList = null;
				if (!newDictionaryMap.isEmpty()) {
					dictionaryMap = new HashMap<String, List<Dictionary>>();
					
					Set<String> keySet = newDictionaryMap.keySet();
					for (String string : keySet) {
						d = new Dictionary();
						d.setCategory(DICTIONARY_CATEGORY);
						d.setOptionText(string);
						d.setOptionValue(string);
						if (dictList == null) {
							dictList = new ArrayList<Dictionary>();
						}
						dictList.add(d);
						dictionaryMap.put(DICTIONARY_CATEGORY, dictList);
					}
				}
				
				loadDictionaryStaff(newDictionaryMap);
				dictionaryMap.putAll(newDictionaryMap);
				needRefresh = false;
				
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}	
	}
	private void loadDictionaryStaff(
			Map<String, List<Dictionary>> newDictionaryMap) {
		Dictionary d = null;				
		/////////////////////////////////////////////////
		List<Staff> staffs;
		try {
			staffs = staffService.getAllStaff();
		
			List<Dictionary> staffList = null;
			for (Staff staff : staffs) {
				d = new Dictionary();
				d.setCategory(STAFF_CATEGORY);
				d.setOptionText(staff.getStaffNo() + "-" + staff.getStaffName());
				d.setOptionValue(staff.getStaffNo());
				if (staffList == null) {
					staffList = new ArrayList<Dictionary>();
				}
				staffList.add(d);
				newDictionaryMap.put(STAFF_CATEGORY, staffList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Dictionary> getDictionaries() {
		if(dictionaries == null) {
			dictionaries = dictionaryService.findAllDictionary();
		}
		return dictionaries;
	}
	
	public Dictionary getDictionaryByValue(String category,String dictValue) {
		List<Dictionary> dictionary = getDictionary(category);
		for (Dictionary dict : dictionary) {
			if(dict.getOptionValue().equals(dictValue)) {
				return dict;
			}
		}
		return null;
	}
	
	public synchronized List<Dictionary> getDictionary(String category) {
		if (dictionaryMap == null) {
			needRefresh = true;
			init();
		}
		return dictionaryMap.get(category);
	}
	
	
	
	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if (event instanceof DictionaryUpdatedEvent) {
			this.dictionaries = null;
			needRefresh = true;
			init();
		} 
		if (event instanceof LoginUserCreatedEvent) {
			loadDictionaryStaff(dictionaryMap);
		} 
	}
}
